<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <script type="text/javascript" src="/imp/static/jquery.js"></script>

    <style type="text/css">
      html {
        overflow: auto;
      }
      html, body, div, iframe {
        margin: 0px;
        padding: 0px;
        height: 100%;
        border: none;
      }
      iframe {
        display: block;
        width: 100%;
        border: none;
        overflow-y: auto;
        overflow-x: hidden;
      }
    </style>
  </head>

  <body>
    <iframe id="content" name="content" src="/imp/static/loading.html" frameborder="0" marginheight="0" marginwidth="0" width="100%" height="100%" scrolling="auto">
    </iframe>

    <script type="text/javascript">
      var buffer = window.location.pathname.split('/')[3];
      var max_period = 60000;
      var min_period = 1000;
      var next_period = min_period;
      var alpha = 1.2;
      var current_id = '-1';
      $('head').append($('<title/>').text(decodeURI(buffer)));

      var nextTimeout = function() {
        var next = next_period;
        next_period = Math.min(max_period, next_period * alpha);
        return next;
      };

      var resetTimeout = function() {
        next_period = min_period;
      };

      var frameToDocument = function(iframe) {
        return (iframe.contentDocument) ? iframe.contentDocument : iframe.Document;
      };

      var printIframe = function(data) {
        var iframeJQ = $('#content');
        var doc = frameToDocument(iframeJQ[0]);
        var iwindow = iframeJQ[0].contentWindow;
        var offX = iwindow.pageXOffset, offY = iwindow.pageYOffset;
        if (offY && offY >= $(doc.body).height() - $(iwindow).height()) {
          offY = null; // follow the end of the buffer
        }
        doc.open();
        doc.write(data);
        doc.close();
        iwindow.scrollTo(offX, offY != null ? offY : $(doc.body).height());
      };

      var setIframe = function(count, newText) {
        if(!count) {
          // error parsing client result
          printIframe('0', 'error parsing the response from emacs');
        } else {
          current_id = count;
          printIframe(newText);
          lastText = newText;
        }
      };

      var refresh = function() {
        var url = "/imp/buffer/" + buffer;

        var gotData = function(data, status, xhr) {
          resetTimeout();
          setIframe(xhr.getResponseHeader("X-Imp-Count"), data);
          refresh();
        };

        var errorRetry = function() {
          setTimeout(refresh, nextTimeout());
        };

        $.get(url + '?id=' + current_id, gotData).error(errorRetry);
      };

      $(document).ready(function() {
        var iframeJQ = $('#content');
        iframeJQ.load(function() {
          // now we need to tweak the stylesheet links so that firefox will refresh them properly
          $('link', frameToDocument(iframeJQ[0])).each(function(index, el) {
            var href = $(el).attr('href');
            // Only refresh impatient-mode hosted content
            if(href && !/^[a-zA-z]+:\/\//.exec(href)) {
              $(el).attr('href', $(el).attr('href') + '?' + new Date().getTime());
            }
          });
        });

        refresh();
      });
    </script>
  </body>
</html>
